package com.mccree.nio;

import io.netty.buffer.ByteBuf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel01 {
    /**
     * https://dongzl.github.io/netty-handbook/#/README
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        FileOutputStream outputStream = new FileOutputStream("D:\\临时\\netty文件\\file01.txt");
        String str = "Hello nio";
        //通过fos获得对应的fileChannel
        FileChannel channel = outputStream.getChannel();

        //创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);


        //将str放入缓冲区
        byteBuffer.put(str.getBytes());

        //读写反转
        byteBuffer.flip();

        //将bytebuffer写入缓冲区
        channel.write(byteBuffer);
        outputStream.close();
    }
}
